package gemini.castor.ui.client.page.content.home.register.inputaccount;

import gemini.basic.model.Distributor;
import gemini.basic.model.Person;
import gemini.basic.model.User;
import gemini.castor.ui.client.mvp.single.CastorWidgetDisplay;
import gemini.castor.ui.client.mvp.single.CastorWidgetPresenter;
import gemini.castor.ui.client.page.content.home.register.RegisterEvent;
import gemini.castor.ui.client.page.content.home.register.confirminfo.ConfirmInfoFormObject;
import gemini.castor.ui.client.utils.PlaceConstants;
import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.inject.Inject;

public class InputAccountFormPresenter extends
		CastorWidgetPresenter<InputAccountFormPresenter.Display> {

	private InputAccountFormObject inputObject;
	private Object outputObject;
	private Distributor inputDistributor;

	@Inject
	public InputAccountFormPresenter(Display display, EventBus globalEventBus) {
		super(display, globalEventBus);
	}

	public interface Display extends CastorWidgetDisplay {
		HasClickHandlers getCancelClickHandlers();
		HasClickHandlers getRegisterClickHandlers();
		void setObject(InputAccountFormObject inputObject);
		boolean isValidate();
	}

	public static final Place PLACE = new Place(PlaceConstants.PLACE_REGISTER);

	@Override
	protected void onBind() {

		registerHandler(display.getRegisterClickHandlers().addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						if(display.isValidate()){
							createNewUser();
						}
					}
				}));
		
		registerHandler(display.getCancelClickHandlers().addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						localEventBus.fireEvent(new RegisterEvent(
								RegisterEvent.ForwardType.CANCEL));
					}
				}));
		
		display.loadData();
	}

	protected void createNewUser() {
		
		User user = new User();
		user.setPassword(inputObject.getPin());
		user.setPerson(inputDistributor.getPersons().get(0));
		user.setUsername(inputObject.getUsername());
		
		ConfirmInfoFormObject confirmObject = new ConfirmInfoFormObject();
		confirmObject.setUser(user);
		confirmObject.setDistributorCode(inputDistributor.getCode());
		confirmObject.setEmail(inputObject.getEmail());
		confirmObject.setFullName(inputObject.getFullName());
		confirmObject.setLanguage(inputObject.getLanguage());
		
		this.outputObject = confirmObject;
		localEventBus.fireEvent(new RegisterEvent(
				RegisterEvent.ForwardType.REGISTER));
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}
	
	@Override
	public void setInputObject(Object o){
		inputDistributor = (Distributor) o;
		inputObject = new InputAccountFormObject();
		inputObject.setDistributorCode(inputDistributor.getCode());
		
		Person person = inputDistributor.getPersons().get(0);
		inputObject.setDistributorEmail(person.getEmail());
		inputObject.setDistributorPin(inputDistributor.getPin());
		
		inputObject.setFullName(person.getFirstName() + " "
				+ person.getLastName());

		if (inputDistributor.getPersons().size() > 1) {
			Person spouse = inputDistributor.getPersons().get(1);
			inputObject.setSpouseFullName(spouse.getFirstName() + " "
					+ spouse.getLastName());
		}
		
		display.setObject(inputObject);
	}

	@Override
	public Object getOutputObject() {
		return outputObject;
	}
}
